package com.example.groupchatdemo.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.text.SimpleDateFormat;

/**
 * @Author: ZhaoJie
 * @Date: 2022/3/28
 * @Description :
 * @Modify :
 * ===================================================
 * Modifier        Modifytime        Description
 * ===================================================
 */
public class ChatGroupServerHandler extends SimpleChannelInboundHandler<String> {

    /**
     *   定义一个channle 组，管理所有的channel
     *   GlobalEventExecutor.INSTANCE) 是全局的事件执行器，是一个单例
     */
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * @author: zhaojie
     * @date: 2022/3/28
     * @description:
     * @method: handlerAdded: 连接建立时将channel放入channel group中
     * @param:
     * @class:  ChatGroupcServerHandler
     * @return:
     */
    @Override
    public void handlerAdded(ChannelHandlerContext context) {
        Channel channel = context.channel();
        // 当有新用户加入时向所有的用户发送消息
        channelGroup.writeAndFlush("{客户端}" + channel.remoteAddress() + "： 加入聊天");
        channelGroup.add(channel);

    }

    /**
     * @author: zhaojie
     * @date: 2022/3/28
     * @description:
     * @method: handlerRemoved: 客户端离开时向其他用户发送消息
     * @param:
     * @class:  ChatGroupcServerHandler
     * @return:
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext context)  throws Exception {
        Channel channel = context.channel();
        channelGroup.writeAndFlush("{客户端}" + channel.remoteAddress() + "： 离开聊天");
        System.out.println("channelGroup size" + channelGroup.size());
    }

    /**
     * @author: zhaojie
     * @date: 2022/3/28
     * @description: 
     * @method: channelActive: //表示channel 处于活动状态, 提示 xx上线
     * @param: 
     * @class:  ChatGroupcServerHandler
     * @return: 
     */
    @Override
    public void channelActive(ChannelHandlerContext context) throws Exception {
        //这个是给服务端看的，客户端上面已经提示xxx加入群聊了
        System.out.println(context.channel().remoteAddress() + " 上线了~");
    }

    /**
     * @author: zhaojie
     * @date: 2022/3/28
     * @description:
     * @method: channelInactive: 表示channel 处于不活动状态, 提示 xx离线了
     * @param:
     * @class:  ChatGroupcServerHandler
     * @return:
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        System.out.println(ctx.channel().remoteAddress() + " 离线了~");
    }

    /**
     * @author: zhaojie
     * @date: 2022/3/28
     * @description:
     * @method: channelRead0: channel 得到数据后的操作
     * @param:
     * @class:  ChatGroupcServerHandler
     * @return:
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String message) throws Exception {
        Channel selfChannel = channelHandlerContext.channel();
        //向其他的客户端发送消息
        channelGroup.forEach(channel -> {
            if (channel != selfChannel) {
                channel.writeAndFlush("[客户端]" + channel.remoteAddress() + ":" + message + "\n");
            } else {
                channel.writeAndFlush("[自己]" + channel.remoteAddress() + ":" + message + "\n");

            }
        });
    }

    /**
     * @author: zhaojie
     * @date: 2022/3/28
     * @description:
     * @method: exceptionCaught: 异常出现是执行的操作
     * @param:
     * @class:  ChatGroupcServerHandler
     * @return:
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //关闭通道
        ctx.close();
    }

}
